package com.vertigo.fever;

import javax.microedition.khronos.opengles.GL10;

import com.vertigo.fever.TileTriangle.Direction;

public class World {

	private static final String[] LEVEL_1 = {
		"BBBBBBB",
		"BBBBBBB",
		"LBBFBBR",
		"LBBBBBR",
		"LBBCBBR",
		"LBB BBR",
		"LBB BBR",
		"LBB BBR",
		"LBBFBBR",
		"LBBFBBR",
		"LBBFBBR",
		"SSSSSSS",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"SSSSSSS",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		" LBBBR ",
		" LBBBR ",
		" LBBBR ",
		" LBBBR ",
		" LBBBR ",
		" LBBBR ",
		" LBBBR ",
		" LBBBR ",
		"  BBB  ",
		"   B   ",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		"LBBBBBR",
		" LBBBR ",
		" LBBBR ",
		" LBBBR ",
		" LBBBR ",
		" LBBBR ",
		" LBBBR ",
		" LBBBR ",
		" LBBBR ",
		"  BBB  ",
		"   B   ",
		"   B   ",
		"   B   ",
		"   B   ",
		"   B   ",
		"   B   ",
		"   B   ",
		"   B   ",
		"   B   ",
		"   B   ",
		"   B   ",
		"   B   ",
		"   B   ",
		"   B   ",
		"   B   ",
		"   B   ",
		"   B   ",
		"   B   ",
	};
	
	private static final int MAX_COLUMNS = 7;
	
	private static final String[][] LEVELS = {LEVEL_1};
	
	static TileFactory tileFactory = new TileFactory();
	
	static {
		tileFactory.add(new TileSquare(0, 'B'));
		tileFactory.add(new TileSquare(1, 'X'));
		tileFactory.add(new TileSquare(2, 'S'));
		tileFactory.add(new TileTriangle(3, 'R', Direction.RHT));
		tileFactory.add(new TileTriangle(4, 'L', Direction.LFT));
		tileFactory.add(new TileTriangle(5, 'F', Direction.FWD));
		tileFactory.add(new TileCrate(0, 'C'));
	}
	
	public Tile getTile(int level, int columnId, int rowId) {
		if (columnId < 0 || columnId >= MAX_COLUMNS) {
			return null;
		}
		if (rowId < 0 || rowId >= LEVELS[level].length) {
			return null;
		}
		char tileCode = LEVELS[level][rowId].toCharArray()[columnId];
		
		return tileFactory.get(tileCode);
	}
	
	public Tile getTile(int level, float x, float y, float z) {
		// TODO NORMALISE POSITIONS SO NOT NEGATIVE
		
		int rowId = Math.round(0.5f - z / Tile.WIDTH);
		int columnId = Math.round(0.5f - x / Tile.WIDTH);
		return getTile(level, columnId, rowId);
	}
	
	public int getLevelLengh(int level) {
		return LEVELS[level].length;
	}
	
	public int getLevelWidth(int level) {
		return  LEVELS[level][0].length();
	}
	
	public void draw(GL10 gl, int row, int level)
    {
		float resetX = getLevelWidth(level) * Tile.WIDTH;
    	for (int r=row; r<getLevelLengh(level); r++) {
        	for (int x=0; x<getLevelWidth(level); x++) {
        		Tile t = getTile(level, x, r);
        		if (t!=null) t.draw(gl);
        		gl.glTranslatef(Tile.WIDTH, 0, 0);
        	}
        	gl.glTranslatef(-resetX,0,-Tile.WIDTH);
        }
    }

	public Tile getTile(int level, Vector p) {
		return getTile(level, p.x+1, p.y, -p.z);
	}
}
